package cn.edu.nuc.fourth.controller;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.nuc.fourth.dto.PageInfo;
import cn.edu.nuc.fourth.model.Announce;
import cn.edu.nuc.fourth.model.Aproject;
import cn.edu.nuc.fourth.services.interfaces.AdminService;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private AdminService service;

	@RequestMapping("/announceadd")
	public String initAddanno() {

		return "admin/annoAdd";
	}

	@RequestMapping("/announcelist")
	public String announceList(@RequestParam(name = "current", defaultValue = "1") Integer current, Model model) {

		PageInfo pageInfo = service.announceList(current);
		model.addAttribute("page", pageInfo);
		return "admin/announce";
	}

	@RequestMapping(value = "/ajax", method = RequestMethod.POST)
	public @ResponseBody PageInfo ajax(Integer current) {
		PageInfo pageInfo = service.announceList(current);
		return pageInfo;
	}

	@RequestMapping(value = "/announceadd", method = RequestMethod.POST)
	public String annoadd(Announce announce) {

		Integer result = service.announceadd(announce);

		return "redirect:/admin/announcelist";
	}

	@RequestMapping("/initedit")
	public String initeidt(Integer aid, Model model) {

		Announce anno = service.oneAnno(aid);
		model.addAttribute("anno", anno);
		return "admin/annoEdit";
	}

	@RequestMapping(value = "/announceedit", method = RequestMethod.POST)
	public String annoedit(Announce announce) {

		Integer result = service.announceedit(announce);
		return "redirect:/admin/announcelist";
	}

	/**
	 * 发布公告修改状态
	 * 
	 * @return
	 */
	@RequestMapping("/announcepub")
	public String annopub(Announce announce) {

		service.stateeidt(announce);
		return "redirect:/admin/announcelist";
	}

	@RequestMapping(value = "/ajaxannolist", method = RequestMethod.POST)
	public @ResponseBody List<Announce> ajaxpub() {

		List<Announce> announce = service.ajaxlist(1);
		return announce;
	}

	@RequestMapping("/adminup")
	public String forupload() {

		return "admin/adminUpload";
	}

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public String upload(MultipartFile main, MultipartFile province, MultipartFile city, MultipartFile onther,
			HttpServletRequest request) {

		String path = request.getServletContext().getRealPath("/WEB-INF/admin/");
		service.upload(main, province, city, onther, path);

		return "redirect:/admin/projectlist";
	}

	@RequestMapping("/projectlist")
	public String manage(@RequestParam(name = "current", defaultValue = "1") Integer current, Model model) {

		PageInfo pageInfo = service.projectlist(current);
		model.addAttribute("page", pageInfo);
		return "admin/mangeProject";
	}

	@RequestMapping("/deleteproject")
	public String projectdelete(Aproject aproject) {

		System.out.println(aproject);
		service.projectdelete(aproject);
		return "redirect:/admin/projectlist";
	}

	@RequestMapping(value = "/find", method = RequestMethod.POST)
	public String find(String date, Model model) {

		PageInfo p = service.find(date);
		model.addAttribute("page", p);

		return "admin/mangeProject";
	}
}
